﻿/*********************************************************************************************************************
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 *********************************************************************************************************************/
var dtCh= "/";
var minYear=2009;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Dagsetning skal vera á forminu 'mm/dd/yyyy'.")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Vinsamlega sláðu inn gildan mánuð.")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Vinsamlega sláðu inn gildan dag.")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Vinsamlega sláðu inn gilt ártal á bilinu "+minYear+" og "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Vinsamlega sláðu inn gildan dag.")
		return false
	}
return true
}
/*********************************************************************************************************************
* DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
* Ends here.
*********************************************************************************************************************/





/***************************************
 * Okkar eigin skriftur hér fyrir neðan.
 ***************************************/

/***************************************
 * Vartölu tékk fyrir kennitölu.
 ***************************************/
function checkKennitala(k) {
    var kennit = k;

    // pattern to ensure we only have ten digits as input...
    var kennitalaPattern = /^([0-9]{10})$/;

    // first sanity check..
    if (k == 0) {
        return false;
    }
    // second sanity check...
    if (k.charAt(6) == '-') {
        return false;
    }
    // third sanity check...
    if (k.match(kennitalaPattern) == null) {
        return false;
    }

    // we should be okay by now, so let's start working on the input...
    var sum = kennit.charAt(0) * 3;
    sum = sum + kennit.charAt(1) * 2;
    sum = sum + kennit.charAt(2) * 7;
    sum = sum + kennit.charAt(3) * 6;
    sum = sum + kennit.charAt(4) * 5;
    sum = sum + kennit.charAt(5) * 4;
    sum = sum + kennit.charAt(6) * 3;
    sum = sum + kennit.charAt(7) * 2;

    // let's find out what vartala we got...
    var vartala = 11 - (sum % 11);

    // vartala should equal kennit.charAt(8)
    if (vartala == kennit.charAt(8)) {
        return true;
    } else {
        if (vartala == 11) {
            if (0 == kennit.charAt(8)) {
                return true;
            } else {
                return false; // kennitala is not correct
            }
        } else {
            return false; // kennitala is not correct
        }
    }
}

/***************************************
 * Athugar hvort strengur sé tölustafir
 ***************************************/ 
function isNumber(string) {
    if (!string)
        return false;
        
    var filter=/^([0-9][0-9])$/i
	if (filter.test(string)) {
	    return true;
	} else {
	    return false;
	}
}

/***************************************
 * Athugar hvort strengur sé bókstafir
 ***************************************/
function isCharacter(string) {
    if (!string)
        return false;
        
    var filter = /^([A-Za-z]{1-20})$/i
    if (filter.test(string)) {
        return true;
    } else {
        return false;
    }
}

/***************************************
 * Athugar hvort strengur innihaldi sértákn
 ***************************************/
function isProper(string) {
    if (!string)
        return false;
        
	var iChars = "*|,\":<>[]{}`\';()@&$#%";

	for (var i = 0; i < string.length; i++) {
	    if (iChars.indexOf(string.charAt(i)) != -1)
	        return false;
	}
	
    return true;
}

/***************************************
 * Athugar hvort bílnúmer sé á réttu formi
 ***************************************/
function isProperNumberPlate(string) {
    if (!string)
        return false;

	var filter = /^[A-Z]{2}[A-Z0-9][0-9]{2}$/;
	if (string.match(filter)) {
	    return true;
	} else {
	    return false;
	}

}

/***************************************
 * Athuga hvort fjarlægð sé rétt skráð og fylgi 
 * lágmarks fjarlægðarreglu
 ***************************************/
function isProperDistance(string) {
    if (!string) 
        return false;

    var iChars = "*|,\":<>[]{}`\';()@&$#%";

    for (var i = 0; i < string.length; i++) {
        if (iChars.indexOf(string.charAt(i)) != -1)
            return false;
    }

    if (parseFloat(string) < parseFloat('0.5')) 
        return false;
    
    return true;
}

/***************************************
 * Athuga hvort ekki hafi verið sleginn 
 * inn ákveðinn fjöldi sæta
 ***************************************/ 
function isProperSeats(string) {
    if (!string) 
        return false;

    if ((string < 1) || (string > 75)) 
        return false;

    return true;
}

/***************************************
 * Athuga hvort kostnaður sé örugglega 
 * aðeins tölustafir og ekki undir 0
 ***************************************/
function isProperCost(string) {
    if (!string)
        return false;

    if (!string.match(/^\d+$/))
        return false;
    
    if (parseInt(string) < parseInt('0')) 
        return false;
    
    return true;
}

/***************************************
 * Athuga hvort tölvupóstfang sé löglegt
 ***************************************/
function isProperEmail(string) {
    if (!string)
        return false;

    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (string.match(filter)) {
        return true;
    } else {
        return false;
    }
}

/***************************************
 * Athuga hvort notandanafn sé rétt
 ***************************************/
function isProperUsername(string) {
    if (!string)
        return false;

    var filter = /^[A-Za-z0-9_]{2,20}$/;
    if (string.match(filter)) {
        return true;
    } else {
        return false;
    }              
}

/***************************************
 * Athuga hvort lykilorð sé rétt
 ***************************************/
function isProperPassword(string) {
    if (!string)
        return false;

    var filter = /^[A-Za-z0-9!@#$%^&*()_]{6,20}$/;
    if (string.match(filter)) {
        return true;
    } else {
        return false;
    }
}

/***************************************
 * Athuga hvort fornafn og/eða eftirnafn sé rétt
 ***************************************/
 function isProperFirstOrLastname(string) {
    if (!string)
        return false;

    if ((string.length < 2) || (string.length > 64)) {
        return false;
    }

    var iChars = "*|,\":<>[]{}`\';()@&$#%0123456789";

    for (var i = 0; i < string.length; i++) {
        if (iChars.indexOf(string.charAt(i)) != -1)
            return false;
    }

    return true;
}

/***************************************
 * Ganga úr skugga um að hægt sé að senda formið
 * niður í serverinn til að vinna úr því.
 ***************************************/
function isReady(form) {

    if (isProper(form.DestFromName.value) == false) {
	    alert("Vinsamlega sláðu inn brottfararstað.");
	    form.DestFromName.focus();
	    return false;
	}
	
    if (isProper(form.DestToName.value) == false) {
	    alert("Vinsamlega sláðu inn áfangastað. (skamm skamm Sigrún)");
	    form.DestToName.focus();
	    return false;
	}

    if (isDate(form.datepicker.value) == false) {
	    alert("Dagseting er á röngu formi. Formið er (mm/dd/yyyy)");
	    form.datepicker.focus();
	    return false;
	}
	
	if (isProperSeats(form.AvailableSeats.value) == false) {
	    alert("Þú verður að velja fjölda farþega í ferð.");
	    form.AvailableSeats.focus();
	    return false;
	}

	if (isProperDistance(form.Distance.value) == false) {
	    alert("Fjarlægð má aðeins innihalda tölustafi og ekki minna en 0.5 kílómeter.");
	    form.Distance.focus();
	    return false;
	}
	
    if (isProperCost(form.Cost.value) == false) {
	    alert("Kostnaður má aðeins innihalda tölustafi.");
	    form.Cost.focus();
	    return false;
	}

    if (isProperNumberPlate(form.NumberPlate.value) == false) {
	    alert("Bílnúmer verður að vera með hástöfum. Dæmi SR537 eða SRX12");
	    form.NumberPlate.focus();
	    return false;
	}

	if (isDate(form.Time.value) == false) {
	    alert("Dagseting er á röngu formi. Formið er (mm/dd/yyyy)");
	    form.Time.focus();
	    return false;
	}

	if (isProperPassword(form.currentPassword.value) == false) {
	    alert("Lykilorð verður að innihalda frá 6 til 20 stafi.\nLeyfilegir stafir eru 'A-Za-z0-9!@#$%^&*()_'");
	    form.currentPassword.focus();
	    return false;
	}
	
	if (isProperPassword(form.newPassword.value) == false) {
	    alert("Lykilorð verður að innihalda frá 6 til 20 stafi.\nLeyfilegir stafir eru 'A-Za-z0-9!@#$%^&*()_'");
	    form.newPassword.focus();
	    return false;
	}
	
	if (isProperPassword(form.confirmPassword.value) == false) {
	    alert("Lykilorð verður að innihalda frá 6 til 20 stafi.\nLeyfilegir stafir eru 'A-Za-z0-9!@#$%^&*()_'");
	    form.confirmPassword.focus();
	    return false;
	}
	
	if (isProperPassword(form.password.value) == false) {
	    alert("Lykilorð verður að innihalda frá 6 til 20 stafi.\nLeyfilegir stafir eru 'A-Za-z0-9!@#$%^&*()_'");
	    form.password.focus();
	    return false;
	}

	if (isProperUsername(form.username.value) == false) {
	    alert("Notandanafn verður að innihalda frá 3 til 20 stafi.\nLeyfilegir stafir eru 'A-Za-z0-9_'");
	    form.username.focus();
	    return false;
	}

	if (isProperEmail(form.email.value) == false) {
	    alert("Tölvupóstfang er ólöglegt.\nVinsamlega sláið inn rétt tölvupóstfang.");
	    form.email.focus();
	    return false;
	}

	if (isProperFirstOrLastname(form.fistname.value) == false) {
	    alert("Nafn má ekki innihalda tölustafi eða sértákn.");
	    form.firstname.focus();
	    return false;
	}

	if (isProperFirstOrLastname(form.lastname.value) == false) {
	    alert("Nafn má ekki innihalda tölustafi eða sértákn.");
	    form.lastname.focus();
	    return false;
	}

	if (isProper(form.TextBoxSearch.value) == false) {
	    alert("Leitarstrengur má ekki innihalda sértákn.");
	    form.TextBoxSearch.focus();
	    return false;
	}
	
	return true;
}


